<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;
use app\common\model\Order as OrderModel;
use think\Request;

/**
 * 订单页面
 */
class Pay extends \think\Controller{

  public function notify(){
			$xml = $GLOBALS["HTTP_RAW_POST_DATA"];
			$xml = new \SimpleXMLElement($xml);
			$xml || exit;
			$data = array();
			foreach ($xml as $key => $value) {
				$data[$key] = strval($value);
			}
      trace($data,'error');
			$Order = model('Order');

      $result = OrderModel::where('out_trade_no', $data['out_trade_no'])->find();
      if($result['is_sendsns'] != 1){
        // sendSms($result['nick_name'], $result['create_time'], $result['out_trade_no'], '15885108322');
        // sendSms($result['nick_name'], $result['create_time'], $result['out_trade_no'], '13638516023');
        $rebateinfo = db('member')->where('uid',$result['rebate_code'])->find();
    		if($rebateinfo['mobile']){
    		 sendSms2($result['nick_name'], $result['create_time'],substr($result['out_trade_no'],-4),(string)$result['total'].config('pay')[$result['pay']],$rebateinfo['username'], $rebateinfo['mobile']);
    	 }
    	 $reusername = $rebateinfo['username'] ? $rebateinfo['username'] : '未设置';
    	//  sendSms2($result['nick_name'], $result['create_time'],substr($result['out_trade_no'],-4),(string)$result['total'].config('pay')[$result['pay']],$reusername, '15885108322');
    	//  sendSms2($result['nick_name'], $result['create_time'],substr($result['out_trade_no'],-4),(string)$result['total'].config('pay')[$result['pay']],$reusername, '13638516023');

       if($result['index_type'] == 2){
         //产地直发
         $sendSmsPhones = explode(',',config('sendsms_m3'));
       }elseif($result['index_type'] == 1){
         //预约商品
         $sendSmsPhones = explode(',',config('sendsms_m2'));
       }else{
         $sendSmsPhones = explode(',',config('sendsms_m1'));
       }
       foreach ($sendSmsPhones as $value) {
         sendSms2($result['nick_name'], $result['create_time'],substr($result['out_trade_no'],-4),(string)$result['total'].config('pay')[$result['pay']],$reusername, (string)$value);
       }

       //查询订单发sendsms_phones 送短信
      //  $sendsmsPhones = json_decode($result['sendsms_phones'], true);
      //  foreach ($sendsmsPhones as $value) {
      //    sendSms2($result['nick_name'], $result['create_time'],substr($result['out_trade_no'],-4),(string)$result['total'].config('pay')[$result['pay']],$reusername, (string)$value);
      //  }

        //  //余额支付和微信支付减余额
   		// 	if($result['pay'] == 3){
   		// 		   db('WxUser')->where('id',$result->user_id)->->setDec('balance', $result['total']);
   		// 	}elseif($result['pay'] == 1){
   		// 		  db('WxUser')->where('id',$result->user_id)->->update(['balance' => 0]);
   		// 	}

      OrderModel::where('out_trade_no', $data['out_trade_no'])->update(['buystatus' => 1, 'is_sendsns'=>1]);
      }







	}

/**
 * 线下支付
 * @return [type] [description]
 */
public function notify2(){
		$xml = $GLOBALS["HTTP_RAW_POST_DATA"];
		$xml = new \SimpleXMLElement($xml);
		$xml || exit;
		$data = array();
		foreach ($xml as $key => $value) {
			$data[$key] = strval($value);
		}
    trace($data,'error');
		$Order = model('Order');

    $result = OrderModel::where('out_trade_no', $data['out_trade_no'])->find();
    if($result['is_sendsns'] != 1){
      $rebateinfo = db('member')->where('uid',$result['rebate_code'])->find();
      if($rebateinfo['mobile']){
       sendSms2($result['nick_name'], $result['create_time'], substr($result['out_trade_no'],-4),(string)$result['total'].config('pay')[$result['pay']],$rebateinfo['username'], $rebateinfo['mobile']);
     }
     $reusername = $rebateinfo['username'] ? $rebateinfo['username'] : '未设置';
     sendSms2($result['nick_name'], $result['create_time'], substr($result['out_trade_no'],-4),(string)$result['total'].config('pay')[$result['pay']],$reusername, '15885108322');
    // sendSms2($result['nick_name'], $result['create_time'], substr($result['out_trade_no'],-4),(string)$result['total'].config('pay')[$result['pay']],$reusername, '13638516023');

      db('WxUser')->where('id',$result->user_id)->setInc('integral', $result->total);      
 
      //发送给会员用户用户
      $userinfo = db('WxUser')->where('id',$result->user_id)->find();      if($userinfo['useredsum'] >0 && $result['total']>=1){      db('WxUser')->where('id',$result->user_id)->setDec('useredsum', 1);      }//       if ($userInfo['useredsum'] >0){//           db('WxUser')->where('id',$result->user_id)->setDec('useredsum', 1);//       }      
      if(isset($userinfo['phone']) && !empty($userinfo['phone'])){
          sendcomSms('SMS_67655286', ['user'=>$userinfo['nickname'], 'time'=>$result['create_time'], 'money'=>(string)$result->total, 'balance'=>(string)$userinfo['balance'], 'integral'=>(string)$userinfo['integral']], (string)$userinfo['phone']);
      }

      //插入行为日志
			$datal['user_id']     = $result->user_id;
			$datal['action_ip']   = ip2long(get_client_ip());
			$datal['model']       = 'ActionLog';
			$datal['record_id']   = $result->id;
			$datal['create_time'] = time();
			$datal['remark'] = time_format($datal['create_time']).'线下付款获得'.$result->total.'积分';
			db('ActionLog')->insert($datal);
      OrderModel::where('out_trade_no', $data['out_trade_no'])->update(['buystatus' => 4, 'is_sendsns'=>1]);
    }


}

/**
 * 自定义支付
 * @return [type] [description]
 */
public function notify3(){
		$xml = $GLOBALS["HTTP_RAW_POST_DATA"];
		$xml = new \SimpleXMLElement($xml);
		$xml || exit;
		$data = array();
		foreach ($xml as $key => $value) {
			$data[$key] = strval($value);
		}
    trace($data,'error');
		$Order = model('Order');

    $result = OrderModel::where('out_trade_no', $data['out_trade_no'])->find();
    if($result['is_sendsns'] != 1){
      $rebateinfo = db('member')->where('uid',$result['rebate_code'])->find();
      if($rebateinfo['mobile']){
       sendSms2($result['nick_name'], $result['create_time'], substr($result['out_trade_no'],-4),(string)$result['total'].config('pay')[$result['pay']],$rebateinfo['username'], $rebateinfo['mobile']);
     }
     $reusername = $rebateinfo['username'] ? $rebateinfo['username'] : '未设置';
     sendSms2($result['nick_name'], $result['create_time'], substr($result['out_trade_no'],-4),(string)$result['total'].config('pay')[$result['pay']],$reusername, '15885108322');
     sendSms2($result['nick_name'], $result['create_time'], substr($result['out_trade_no'],-4),(string)$result['total'].config('pay')[$result['pay']],$reusername, '13638516023');

      db('WxUser')->where('id',$result->user_id)->setInc('integral', $result->total);
      //使用余额

      //发送给会员用户用户
      $userinfo = db('WxUser')->where('id',$result->user_id)->find();
      if(isset($userinfo['phone']) && !empty($userinfo['phone'])){
          sendcomSms('SMS_67655286', ['user'=>$userinfo['nickname'], 'time'=>$result['create_time'], 'money'=>(string)$result->total, 'balance'=>(string)$userinfo['balance'], 'integral'=>(string)$userinfo['integral']], (string)$userinfo['phone']);
      }

      //插入行为日志
			$datal['user_id']     = $result->user_id;
			$datal['action_ip']   = ip2long(get_client_ip());
			$datal['model']       = 'ActionLog';
			$datal['record_id']   = $result->id;
			$datal['create_time'] = time();
			$datal['remark'] = time_format($datal['create_time']).'自定义付款获得'.$result->total.'积分';
			db('ActionLog')->insert($datal);
      OrderModel::where('out_trade_no', $data['out_trade_no'])->update(['buystatus' => 1, 'is_sendsns'=>1]);
    }


}

}
